<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>修改企业</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/js/layui/css/layui.css" media="all">
</head>
<body>
<div style="width:80%;height:700px;margin:15px auto;overflow:auto;" id="aaa">
    <form class="layui-form" action="">
        <input type="hidden" class="input-text" id="id" name="id">
        <div class="layui-form-item">
            <label class="layui-form-label">企业名称</label>
            <div class="layui-input-block">
                <input type="text" name="qyname" required  lay-verify="required" placeholder="请输入企业名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">负责人</label>
            <div class="layui-input-block">
                <input type="text" name="fzr" required lay-verify="required" placeholder="请输入负责人姓名" autocomplete="off" class="layui-input">
            </div>
            <!--<div class="layui-form-mid layui-word-aux">辅助文字</div>-->
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">联系方式</label>
            <div class="layui-input-block">
                <input type="text" name="phone" required lay-verify="required" placeholder="请输入联系方式" autocomplete="off" class="layui-input">
            </div>
            <!--<div class="layui-form-mid layui-word-aux">辅助文字</div>-->
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">企业地址</label>
            <div class="layui-input-block">
                <input type="text" name="address" lay-verify="required" placeholder="请输入公司的地址" autocomplete="off" class="layui-input">
            </div>
            <!--<div class="layui-form-mid layui-word-aux">辅助文字</div>-->
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">企业简介</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea" lay-verify="required" type="text" id="message" name="message" placeholder="请输入企业的介绍信息" autocomplete="off" class="layui-input"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">到期时间</label>
            <div class="layui-input-inline">
                <input type="text" name="qyDqtime" id="start_time" lay-verify="required" autocomplete="off" placeholder="请输入到期时间" class="layui-input">
            </div>
        </div>
        <!-- 图片上传 -->
        <div class="layui-upload">
            <button type="button" class="layui-btn" id="test" style="display: inline;float: left;">上传logo</button>
            <div class="layui-upload-list" style="display: inline;margin-left: 30px;">
                <img class="layui-upload-img" name="logo" id="logo" style="width: 200px;height: 150px">
                <p id="demoText"></p>
            </div>
            <input type="hidden" class="input-text" id="picture" name="picture">
        </div>

        <div class="layui-form-item" style="margin-top: 50px">
            <div class="layui-input-block">
                <button class="layui-btn" id="addWuye" lay-submit lay-filter="addWuye">立即修改</button>
                <button type="reset" id="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>

<script src="../../js/layui/layui.js"></script>
<script src="../../js/layui/layui.all.js"></script>
<script src="../../js/token.js"></script>
<script src="../../js/config.js"></script>

<script>
    //url 传参
    function GetQueryString(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var a = decodeURI(window.location.search);
        var r = a.substr(1).match(reg);//search,查询？后面的参数，并匹配正则
        if(r!=null)return  unescape(r[2]); return null;
    }

    window.onload = function(){
        //alert(decodeURI(window.location.href));
        //alert(decodeURI(window.location.search));
        // var wyName = GetQueryString("wyName");
        // console.log("物业名称："+wyName);
        // $('#eee').text(wyName);
        // $('#eee').val(GetQueryString("wyID"));
        $("input[name='id']").val(GetQueryString("id"));
        $("input[name='qyname']").val(GetQueryString("qyname"));
        $("input[name='fzr']").val(GetQueryString("fzr"));
        $("input[name='phone']").val(GetQueryString("phone"));
        $("input[name='address']").val(decodeURIComponent(GetQueryString("address")));
        $("input[name='picture']").val(GetQueryString("logo"));

        var dqtime = GetQueryString("qyDqtime");
        if(dqtime==null || dqtime==""){
            $("input[name='qyDqtime']").val();
        }else{
            $("input[name='qyDqtime']").val(dqtime);
        }
        $("#message").val(GetQueryString("message"));
        $('#logo').attr('src', "/upload/"+GetQueryString("logo"));
        //var wyID = GetQueryString("wyID");
        //$("#cla").find("option[value='wyID']").attr("selected", true);
        //$("#cla option[value=aa]").attr("selected", "selected");
    }

    layui.use(['form' , 'jquery' , 'upload'], function(){
        var form = layui.form;
        var $ = layui.jquery;
        var upload = layui.upload;

        //修改操作
        form.on('submit(addWuye)',function(data){
            data.field.logo=$("input[name='picture']").val();
            data.field.wyID = $("#cla").find("option:selected").val();
            data.field.id = $("#id").val();
            //console.log($("#cla").find("option:selected").val());
            $.ajax({
                url:"/qiye/updateqiye?denglu="+0
                ,type:"POST"
                ,contentType: 'application/json'
                ,data:JSON.stringify(data.field)
                ,success:function (data) {
                    if(data.code==2){
                        layer.msg('该企业名称已存在，请重新输入！');
                    }else if(data.code==4) {
                        layer.msg('请先上传logo！');
                    }else if(data.code==0){
                        layer.close(layer.index);
                        layer.msg('修改成功');
                        setTimeout(function(){
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);//关闭弹出层
                            parent.location.reload();//重新加载父页面表格
                        }, 2000);
                    }else{
                        layer.msg('修改失败！');
                    }
                }
                ,error:function () {
                    console.log("ajax error");
                }
            });
            return false;
        });

        //遍历物业下选框
        $.ajax({
            url: "/wuye/selectwuyestatus"
            , type: "POST"
            , contentType: 'application/json'
            , data: {'status': 1}
            , success: function (data) {
                $('#cla').append(new Option(GetQueryString("wyName"),GetQueryString("wyID"),true));
                $.each(data.data, function (index, item) {
                    $('#cla').append(new Option(item.wyname, item.id));// 下拉菜单里添加元素
                });
                layui.form.render("select");
            }
        });

        //logo上传
        var uploadInst = upload.render({
            elem: '#test'
            , url: '/upload' //改成您自己的上传接口
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#logo').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //判断上传是否成功
                if (res.code == 0) {
                    $("#picture").val(res.data);
                    return layer.msg('上传成功！');
                }else{
                    return layer.msg('上传失败！');
                }
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
    });
</script>
<script>
    layui.use('laydate', function(){
        var laydate = layui.laydate;
        var endDate= laydate.render({
            elem: '#end_time',//选择器结束时间
            type: 'datetime',
            min:"1970-1-1",//设置min默认最小值
            done: function(value,date){
                startDate.config.max={
                    year:date.year,
                    month:date.month-1,//关键
                    date: date.date,
                    hours: 0,
                    minutes: 0,
                    seconds : 0
                }
            }
        });

        //日期范围
        var startDate=laydate.render({
            elem: '#start_time',
            type: 'datetime',
            max:"2099-12-31",//设置一个默认最大值
            done: function(value, date){
                endDate.config.min ={
                    year:date.year,
                    month:date.month-1, //关键
                    date: date.date,
                    hours: 0,
                    minutes: 0,
                    seconds : 0
                };
            }
        });
    });
</script>
</body>
</html>